package com.witmed.mine.view

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Recycler
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.witmed.lib_common.ActivityPath
import com.witmed.lib_common.room.VideoHistory
import com.witmed.lib_common.utils.ACache
import com.witmed.lib_common.utils.ImageUtils
import com.witmed.lib_http.entity.UserEntity
import com.witmed.mine.R
import com.witmed.mine.databinding.FragmentMineBinding
import com.witmed.mine.view.adapter.VideoAdapter
import com.witmed.mine.view.intent.VideoHistroyIntent
import com.witmed.mine.view.viewmodel.VideoHistoryState
import com.witmed.mine.view.viewmodel.VideoHistoryViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 * A simple [Fragment] subclass.
 * Use the [MineFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
@Route(path = ActivityPath.PAGE_MINE)
class MineFragment : Fragment() {

    lateinit var vm:VideoHistoryViewModel
    lateinit var db:FragmentMineBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        db=FragmentMineBinding.inflate(layoutInflater,container,false)
//        val view = inflater.inflate(R.layout.fragment_mine, container, false)

        vm=ViewModelProvider(this).get(VideoHistoryViewModel::class.java)

        val list = mutableListOf<VideoHistory>()
        val videoAdapter =
            VideoAdapter(requireContext(), list, object : VideoAdapter.ItemClickListener {
                override fun onClick(position: Int, entity: VideoHistory) {
                    Log.i("===",entity.title)
                }

            }, object : VideoAdapter.LongClickListener {
                override fun onLongClick(entity: VideoHistory) {
                    Log.i("===",entity.title)
                }

            })
        db.rvHistory.apply {
            adapter=videoAdapter
            layoutManager=LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL,false)
        }

        lifecycleScope.launch {
            vm.videoState.collect{
                when(it){
                    is VideoHistoryState.Sucess->{
                        Log.i("===",it.list.size.toString())

                        list.clear()
                        list.addAll(it.list)
                        videoAdapter.notifyDataSetChanged()
                    }
                    is VideoHistoryState.Error->{
                        Log.i("===",it.msg)
                    }
                    else->{}
                }
            }
        }


        lifecycleScope.launch {
            vm.videoChannel.send(VideoHistroyIntent.selectList(requireContext()))
        }

        db.tvNologin.setOnClickListener {
            ARouter.getInstance().build(ActivityPath.PAGE_LOGIN).navigation()
        }

        return db.root
    }

    override fun onResume() {
        super.onResume()
        var user=ACache.get(requireContext()).getAsObject("user")

        if(user==null){
            db.tvNologin.visibility=View.VISIBLE
            db.groupLogin.visibility=View.GONE
            ImageUtils.loadIamgeCircle(requireContext(),"https://img1.baidu.com/it/u=1045678354,3246580876&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=613",db.ivPhoto)
        }else{
            user=user as UserEntity
            db.tvNologin.visibility=View.GONE
            db.groupLogin.visibility=View.VISIBLE
            db.username.text=user.username
            ImageUtils.loadIamgeCircle(requireContext(),user.headImg?:"https://img1.baidu.com/it/u=1045678354,3246580876&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=613",db.ivPhoto)
        }
    }


}